package unimelb.distSystems.project2;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.security.MessageDigest;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/*
 * Subclass of GUIScreen for registering a new account for the game
 */
public class LoginScreen extends GUIScreen{
	/*
	 * GUI components
	 */
	private JTextField username_tf = new JTextField();
	private JLabel username_text = new JLabel("Username");
	private JPasswordField password_tf = new JPasswordField();
	private JLabel password_text = new JLabel("Password");
	private JButton login_button = new JButton("Login");
	private JButton create_button = new JButton("New account");
	private JButton exit_button = new JButton("Exit");
	private JButton credit_button = new JButton("Credits");
	private ImagePanel image_p = new ImagePanel(Configurations.login_picture);
	
	/*
	 * Constructor
	 */
	public LoginScreen(Game client)
	{
		super();
		initialise_listeners(client);
		
		username_tf.setPreferredSize(new Dimension(260,30));
		password_tf.setPreferredSize(new Dimension(260,30));
	}
	
	/*
	 * @see unimelb.distSystems.project2.GUIScreen#perform_actions_prior_display()
	 */
	public void perform_actions_prior_display()
	{
		//username_tf.setText("");
		//password_tf.setText("");
		
		username_tf.setText("beagle");//TEMP
		password_tf.setText("qwerty");
	}
	
	/*
	 * @see unimelb.distSystems.project2.GUIScreen#perform_actions_prior_exit()
	 */
	public void perform_actions_prior_exit()
	{
		//Do nothing
	}
	
	/*
	 * @see unimelb.distSystems.project2.GUIScreen#display(java.awt.Container)
	 */
	public void display(Container screen_container) {
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		screen_container.setLayout(new FlowLayout());
		
		JPanel title_p = new JPanel();
		title_p.setLayout(new GridBagLayout());		
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		c.ipadx=image_p.getImageWidth();
		c.ipady=image_p.getImageHeight();
		title_p.add(image_p,c);
		c.ipadx=1;
		c.ipady=1;
		screen_container.add(title_p);
		
		JPanel main_p = new JPanel();
		main_p.setLayout(new GridBagLayout());
		screen_container.add(main_p);
		
		c.gridx = 0;
		c.gridy = 0;
		main_p.add(username_text,c);
		
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 2;
		main_p.add(username_tf,c);
		c.gridwidth = 1;
		
		c.gridx = 0;
		c.gridy = 2;
		main_p.add(blank,c);
		
		c.gridx = 0;
		c.gridy = 3;
		main_p.add(password_text,c);
		
		c.gridx = 0;
		c.gridy = 4;
		c.gridwidth = 2;
		main_p.add(password_tf,c);
		c.gridwidth = 1;
		
		c.gridx = 0;
		c.gridy = 5;
		main_p.add(blank2,c);
		
		c.gridx = 0;
		c.gridy = 6;
		main_p.add(create_button,c);
		
		c.gridx = 1;
		c.gridy = 6;
		main_p.add(login_button,c);
		
		c.gridx = 0;
		c.gridy = 7;
		main_p.add(blank3,c);
		
		c.gridx = 0;
		c.gridy = 8;
		c.gridwidth = 2;
		main_p.add(credit_button,c);
		c.gridwidth = 1;
		
		c.gridx = 0;
		c.gridy = 9;
		c.gridwidth = 1;
		main_p.add(blank4,c);
		
		c.gridx = 0;
		c.gridy = 10;
		c.gridwidth = 2;
		main_p.add(exit_button,c);
	}

	/*
	 * @see unimelb.distSystems.project2.GUIScreen#initialise_listeners(unimelb.distSystems.project2.Game)
	 */
	protected void initialise_listeners(final Game client) {
		/*
		 * Exit the game
		 */
		exit_button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				client.perform_exit_operation();
			}
		});
		
		/*
		 * Authenticate the human
		 */
		login_button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String username = username_tf.getText().trim();
				String password = new String(password_tf.getPassword());
				String md5_password = "";
				MessageDigest md;
								
				if(username.equals("") || password.equals(""))
					return;
				
				try {
					md = MessageDigest.getInstance("MD5");
					byte[] passwordBytes = password.getBytes("UTF-8");
					md5_password = md.digest(passwordBytes).toString();
				} catch (Exception e) {
					System.out.println("Error: MessageDigest encounters this error - "+e.getMessage());
				}
				
				////////////////////////////////////
				//COBRA COMMUNICATION
				////////////////////////////////////
				//Pass the details to GameServer and get account object (or null if failed)
				Player player=Configurations.sample_player_object;//TEMP
				try {player.setAvatar(ImageIO.read(new File("images/sample_avatar.png")));}
				catch (IOException e) {}
				
				if(player!=null)//Successful
				{
					client.cache_player(player);
					client.go_to_screen(Game.MENU_SCREEN);
				}
				else //Wrong
					JOptionPane.showMessageDialog(client,"Wrong username/password",
							"Error",JOptionPane.ERROR_MESSAGE);
			}
		});
		
		/*
		 * Go to another screen to register new account for the game
		 */
		create_button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				client.go_to_screen(Game.CREATEACCOUNT_SCREEN);
			}
		});
		
		/*
		 * Our credit of us
		 */
		credit_button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(client,Configurations.credits,
						"Credits",JOptionPane.INFORMATION_MESSAGE);
			}
		});
	}
}
